﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace AppGPS.Helper
{
    public class APIRequest<T>
    {
        /// <summary>
        /// GetRequestAsync send Get method in webs API for get Data
        /// </summary>
        /// <param name="szHost">Sample : http://localhost:80/</param>
        /// <param name="ParametersAPI">Sample : api/User/?Email=final_fantasy1121@yahoo.com</param>
        /// <returns></returns>
        public async Task<T> GetRequestAsync(string szHost, string ParametersAPI)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(szHost);

                client.DefaultRequestHeaders.Accept.Clear();

                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaType.json));
                //var Uri = string.Format("api/Login/?Email={0}", _Email);
                HttpResponseMessage response = await client.GetAsync(ParametersAPI);
                if (response.IsSuccessStatusCode)
                {
                    string Result = await response.Content.ReadAsStringAsync();

                    Debug.WriteLine(Result);
                    var TResult = JsonConvert.DeserializeObject<T>(Result);

                    return (T)TResult;
                }
                return default(T);
            }
        }
        /// <summary>
        /// PostRequestAsync send Post Method in API webs
        /// </summary>
        /// <param name="szHost">Sample : http://localhost:80/</param>
        /// <param name="UrlAPI">Sample : api/User</param>
        /// <param name="Parameter">Sample : User obj=new User(){Name="abc",Id="1231"}; PostRequestAsync(szHost,UrlAPI,obj)...</param>
        /// <returns></returns>
        public async Task<bool> PostRequest(string szHost, string UrlAPI, T Parameter)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(szHost);

                client.DefaultRequestHeaders.Accept.Clear();

                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaType.json));

                // New code:
                var json = JsonConvert.SerializeObject(Parameter);

                StringContent content = new StringContent(json, System.Text.Encoding.UTF8, MediaType.json);

                HttpResponseMessage response = await client.PostAsync(UrlAPI, content);

                var myobject = await response.Content.ReadAsStringAsync();

                if (myobject == "" || myobject == null) return false;
                else
                    return true;
            }
        }
        public async Task<T> PostRequestAsync(string szHost, string UrlAPI, T Parameter)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(szHost);

                client.DefaultRequestHeaders.Accept.Clear();

                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaType.json));

                // New code:
                var json = JsonConvert.SerializeObject(Parameter);

                StringContent content = new StringContent(json, System.Text.Encoding.UTF8, MediaType.json);

                HttpResponseMessage response = await client.PostAsync(UrlAPI, content);

                var myobject = await response.Content.ReadAsStringAsync();

                var TResult = JsonConvert.DeserializeObject<T>(myobject);

                return (T)TResult;
            }
        }
        /// <summary>
        /// PutRequestAsync Send Put Request for change in API Webs
        /// </summary>
        /// <param name="szHost">Sample : http://localhost:80/</param>
        /// <param name="UrlAPI">Sample : api/User</param>
        /// <param name="Parameter">Sample : User obj=new User(){Name="abc",Id="1231"}; PostRequestAsync(szHost,UrlAPI,obj)...</param>
        public async Task<bool> PutRequestAsync(string szHost, string UrlAPI, T Parameter)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(szHost);
                var json = JsonConvert.SerializeObject(Parameter);
                StringContent content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
                HttpResponseMessage response = await client.PutAsync(UrlAPI, content);
                if (response.IsSuccessStatusCode)
                {
                    return true;
                }

            }
            return false;
        }
        public async Task<bool> PutRequestAsync(string szHost, string UrlAPI, string Parameter)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(szHost);
                var json = JsonConvert.SerializeObject(Parameter);
                StringContent content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
                HttpResponseMessage response = await client.PutAsync(UrlAPI, content);
                if (response.IsSuccessStatusCode)
                {
                    return true;
                }

            }
            return false;
        }
        /// <summary>
        /// DeleteRequestAsync Send DeleteRequest for API webs
        /// </summary>
        /// <param name="szHost">Sample: http://localhost:80/</param>
        /// <param name="ParameterAPI">Sample:api/user/?UserID=1</param>
        public bool DeleteRequestAsync(string szHost, string ParameterAPI)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(szHost);

                HttpResponseMessage response = client.DeleteAsync(ParameterAPI).Result;

                if (response.IsSuccessStatusCode)
                {
                    return true;
                }
                else
                    return false;
            }
        }
    }
}
